package com.stlm2.controller;

import com.stlm2.core.common.page.PageView;
import com.stlm2.dems.entity.Cards;
import com.stlm2.dems.entity.CardsCategory;
import com.stlm2.dems.entity.CardsType;
import com.stlm2.dems.entity.User;
import com.stlm2.dems.service.CardsService;
import com.stlm2.dems.service.UserService;
import com.stlm2.util.StringUtils;
import com.stlm2.util.codecontent.CodeContent;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

/**
 * Author：fuchunyu
 * Date：2017/10/17
 * Description：
 */
@Controller
@RequestMapping("/cardsC")
public class CardsController {

    @Resource
    private UserService userService;
    @Resource
    private CardsService cardsService;



    //查询卡券
    @ResponseBody
    @RequestMapping(value = "/searchCards.json",produces = "application/json;charset=UTF-8")
    public String searchCards(ModelMap model, String page){
        PageView pageview =new  PageView();
        int pageNo = 1;
        int pageSize=20;
        if (NumberUtils.isDigits(page) && !"0".equals(page.trim())) {
            pageNo = NumberUtils.toInt(page);
        }
        Cards cards =new Cards();
        pageview= cardsService.getPage(pageNo,pageSize,cards);
        if (pageview!=null){
            model.put("data",cardsService.convert(pageview.getRecords()));
            model.put("recordsTotal", pageview.getRowCount());
            model.put("totalPage", pageview.getPageCount());
        }
        model.put("pageNo", page);
        return CodeContent.returnSuccess(model);
    }

    /**
     * Author：fuchunyu
     * Date：2017/10/18
     * Description：添加卡券
     */
    @ResponseBody
    @RequestMapping(value = "/addCards.json",produces = "application/json;charset=utf-8")
    public String addCards(String token,String name,String cardsType,String money,String validDay,String goodsId,String goodsCategoryId,String cardsCategory){
        User user=userService.getByToken(token);
        if (user==null)
            return CodeContent.returnLogin();
        if(StringUtils.isNullOrTrimEmpty(name))
            return CodeContent.returnFailure("卡券名称不能为空！");
        if(!NumberUtils.isDigits(cardsType))
            return CodeContent.returnFailure("卡券类型不能为空！");
        if(!NumberUtils.isDigits(money))
            return CodeContent.returnFailure("卡券金额不能为空！");
        if(!NumberUtils.isDigits(validDay))
            return CodeContent.returnFailure("卡券过期天数不能为空！");
        if(!NumberUtils.isDigits(cardsCategory))
            return CodeContent.returnFailure("卡券分类不能为空！");
        Cards cards=new Cards();
        cards.setName(name);
        cards.setMoney(new BigDecimal(money));
        cards.setValidDay(Integer.parseInt(validDay));
        cards.setCardsCategory(CardsCategory.getByIntValue(Integer.parseInt(cardsCategory)));
        cards.setCardsState(1);
        cards.setCardsType(CardsType.getByIntValue(Integer.parseInt(cardsType)));
        cards.setCreateTime(new Date());
        cards.setCreateUserId(user.getId());
        if(StringUtils.isNotNullAndEmpty(goodsId)&&NumberUtils.isDigits(goodsId))
            cards.setGoodsId(Integer.parseInt(goodsId));
        if(StringUtils.isNotNullAndEmpty(goodsCategoryId)&&NumberUtils.isDigits(goodsCategoryId))
            cards.setGoodsCategoryId(Integer.parseInt(goodsCategoryId));
        cardsService.addEntity(cards);
        return CodeContent.returnSuccess(null);
    }

    /**
     * Author：fuchunyu
     * Date：2017/10/18
     * Description：更新卡券
     */
    @ResponseBody
    @RequestMapping(value = "/updateCards.json",produces = "application/json;charset=utf-8")
    public String updateCards(String token,Integer id,String name,Integer cardsType,BigDecimal money,Integer validDay,Integer goodsId,Integer goodsCategoryId,Integer cardsCategory) {
        User user = userService.getByToken(token);
        if (user == null)
            return CodeContent.returnLogin();
        Cards cards=cardsService.findEntityById(id);
        if(StringUtils.isNotNullAndEmpty(name))
            cards.setName(name);
        if(cardsType!=null)
            cards.setCardsTypeValue(cardsType);
        if(money!=null)
            cards.setMoney(money);
        if(validDay!=null)
            cards.setValidDay(validDay);
        if(goodsId!=null)
            cards.setGoodsId(goodsId);
        if(goodsCategoryId!=null)
            cards.setGoodsCategoryId(goodsCategoryId);
        if(cardsCategory!=null)
            cards.setCardsCategoryValue(cardsCategory);
        cardsService.updateEntity(cards);
        return CodeContent.returnSuccess(null);
    }

    /**
     * Author：fuchunyu
     * Date：2017/10/18
     * Description：跳转到添加页面
     */
    @RequestMapping("/toAdd.json")
    public String toAdd(ModelAndView model){
        return "/cards/add";
    }
    /**
     * Author：fuchunyu
     * Date：2017/10/18
     * Description：跳转到列表页
     */
    @RequestMapping("/toList.json")
    public String toList(){
        return "/cards/list";
    }
}
